package com.xxkk.gameCore.global.globalServer;

import com.xxkk.net.websocketNetty.Player;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author xxkk
 * @version 1.0.0
 * @title ServerManager
 * @description ʕ•ᴥ•ʔ 对serverContext进行管理，所有的上下文操作放在这儿
 * @create 2023/11/11 14:24
 **/
@Service
public class ServerManager {
    @Autowired
    private ServerContext serverContext;

    /**
     * 玩家登陆，加入玩家到区服，移除正在连接的状态
     *
     * @param player
     * @param areaId
     * @return 如果存在重复登陆则返回旧玩家player，否则返回null
     */
    public Player addPlayer(Player player, int areaId) {
        return this.serverContext.getGameAreaContext(areaId).addLogonPlayer(player);
    }

    /**
     * 玩家退出，从区服中移除玩家
     *
     * @param player
     * @param areaId
     * @return 移除成功则返回移除的player，否则返回null
     */
    public Player rmPlayer(Player player, int areaId) {
        return this.serverContext.getGameAreaContext(areaId).rmLogonPlayer(player);
    }

    /**
     * 移除连接中的玩家
     *
     * @param playerId
     * @return 移除的玩家信息player或null
     */
    public Player rmConnectingPlayer(long playerId) {
        return this.serverContext.getConnectingPlayerMap().remove(playerId);
    }

}
